package client.connection;

import general.helperclasses.logger.Logger;
import general.helperclasses.logger.LoggingLevel;
import general.messages.StringMessage;
import general.messages.UpdateUnitMessage;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class ConnectionHandler extends Thread{
	
	private final int port;
	private final Socket socket;
	private final ObjectInputStream input;
	
	/**
	 * Erzeugt den {@link ConnectionHandler} und startet ihn auch direkt. Start muss nicht aufgerufen werden
	 * @throws IOException 
	 */
	public ConnectionHandler(int port) throws IOException
	{
		this.port = port;
		try {
			Logger.addEntry(LoggingLevel.DEBUG, "Trying to connect to the server...");
			this.socket = new Socket("timmeurer.dyndns.org", port);
			Logger.addEntry(LoggingLevel.DEBUG, "Connected to the server");
			System.out.println("Connected to server");
			
			input = new ObjectInputStream(socket.getInputStream());
			
		} catch (UnknownHostException e) {
			Logger.addEntry(LoggingLevel.INFO, "Could not find host");
			throw e;
		} catch (IOException e) {
			Logger.addEntry(LoggingLevel.SERVE, "Something went wrong while connecting to the server:\n"+e.toString());
			throw e;
		}
	}
	
	/**
	 * 
	 */
	@Override
	public void run() {
		int receiveCount = 0;
		long oldtime = System.currentTimeMillis();
		long newtime = System.currentTimeMillis();
		
		System.out.println("Running!");
		while(!socket.isClosed())
		{
			try {
				//System.out.println("Waiting for message");
				
				Object received = input.readObject();
				
				if (received instanceof StringMessage) {
					String message = ((StringMessage) received).Message;
					System.out.println("Received message from server: "+message);
				}
				
				if (received instanceof UpdateUnitMessage) {
					UpdateUnitMessage unitUpdate = ((UpdateUnitMessage) received);
					receiveCount++;
					
					if(receiveCount % 5000 == 0)
					{
						newtime = System.currentTimeMillis();
						System.out.println("5000 Objekte erhalten, durchnittlicher Intervall: " + ((float)(newtime-oldtime)/(float)5000));
						oldtime = newtime;
					}
					//System.out.println("Received message from server: "+unitUpdate.toString());
				}
				
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			Logger.printLast();
			
		}
		
		try {
			input.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Logger.printLast();
		
	}
	
	public static void main(String[] args) throws IOException
	{
		//System.setProperty("org.lwjgl.librarypath",System.getProperty("user.dir") + "/native/");
		Logger.setDebugLevel(LoggingLevel.ALL);
		ConnectionHandler conhandler = new ConnectionHandler(14567);
		conhandler.start();
	}

}
